package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.HandleDto;
import com.huaxiafinance.management.dto.request.WithdrawalsDto;
import com.huaxiafinance.management.service.PresentProcessingService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Controller("presentProcessingController")
@RequestMapping("/presentProcessing")
public class PresentProcessingController {

  protected Logger logger = Logger.getLogger(PresentProcessingController.class);
  @Autowired
  private PresentProcessingService presentProcessingService;

  /**
   * 获取提现状态名称列表
   */
  @RequestMapping(value = "/queryStateList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryStateList() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = presentProcessingService.queryStateList();
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("请求提现状态名称列表异常原因：" + e);
    }
    logger.info("请求提现状态名称列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();

  }


  /**
   * 获取提现处理列表
   */
  @RequestMapping(value = "/queryWithdrawals", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryWithdrawals(@Valid @RequestBody HandleDto dto, BindingResult bindingResult) {
    logger.info("请求提现处理列表参数：" + dto.toString());
    MessageResult messageResult = new MessageResult();
    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
    } else {
      try {
        messageResult = presentProcessingService.queryWithdrawals(dto);
      } catch (Exception e) {
        messageResult.setFailureResult();
        logger.info("请求提现处理列表异常原因：" + e);
      }
    }
    logger.info("请求提现处理列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }


  /**
   * 展示提现详情
   */
  @RequestMapping(value = "/queryWithdrawalsDetails", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryWithdrawalsDetails(HttpServletRequest request) {
    logger.info("请求展示提现详情参数：" + request.toString());
    MessageResult messageResult = new MessageResult();
    String id = request.getParameter("id");
    try {
      messageResult = presentProcessingService.queryWithdrawalsDetails(id);
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("请展示提现详情异常原因：" + e);
    }
    logger.info("请求展示提现详情返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 修改提现申请
   */
  @RequestMapping(value = "/updateWithdrawals", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateWithdrawals(@Valid @RequestBody WithdrawalsDto dto, BindingResult bindingResult) {
    logger.info("修改提现申请参数：" + dto.toString());
    MessageResult messageResult = new MessageResult();
    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
    } else {
      try {
        messageResult = presentProcessingService.updateWithdrawals(dto);
      } catch (Exception e) {
        messageResult.setFailureResult();
        logger.info("修改提现申请异常原因：" + e);
      }
    }
    logger.info("修改提现申请返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }


}
